package com.salesforce.samples.templateapp;

import java.util.ArrayList;

import android.app.Activity;
import android.opengl.Visibility;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.salesforce.samples.templateapp.adapter.SiteAdapter;
import com.salesforce.samples.templateapp.model.InventoryModel;

public class WarehouseFragment extends Fragment implements OnItemClickListener {
	
	LinearLayout warehouseDetails,siteDetails;
	TextView tvSiteName,tvCity,tvState,tvStreet,tvCountry,tvZip,tvSitePhone,tvSiteFax,tvEmail,tvWebsite;
	private TextView tvWareHouseName,tvDescription,tvGeoLocation;
	ListView lv;
	ArrayList<InventoryModel> list ;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.warehouse_fragment, null);
	}
	
	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		super.onAttach(activity);
		
		
		
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		tvWareHouseName = (TextView) getView().findViewById(R.id.tvWarehouseName);
		tvDescription = (TextView) getView().findViewById(R.id.tvDescription);
		tvGeoLocation = (TextView) getView().findViewById(R.id.tvgeoLocation);
		lv = (ListView) getView().findViewById(R.id.siteListview);
		lv.setOnItemClickListener(this);
		
		warehouseDetails = (LinearLayout) getView().findViewById(R.id.llayoutWarehouseDetails);
		siteDetails = (LinearLayout) getView().findViewById(R.id.llayoutSiteDetails);
		tvSiteName = (TextView) getView().findViewById(R.id.tvSiteName);
		tvCity = (TextView) getView().findViewById(R.id.tvCity);
		tvState = (TextView) getView().findViewById(R.id.tvState);
		tvStreet = (TextView) getView().findViewById(R.id.tvStreet);
		tvCountry = (TextView) getView().findViewById(R.id.tvCountry);
		tvZip = (TextView) getView().findViewById(R.id.tvZip);
		tvSitePhone = (TextView) getView().findViewById(R.id.tvSitePhone);
		tvSiteFax = (TextView) getView().findViewById(R.id.tvSiteFax);
		tvEmail = (TextView) getView().findViewById(R.id.tvSiteEmail);
		tvWebsite = (TextView) getView().findViewById(R.id.tvSiteWebsite);
	}
	
	
	public void getWarehouseDetails(int Position){      
		
		InventoryModel mobel = Inventory.InventoryList.get(Position);
		
		tvWareHouseName.setText(mobel.getName());
		tvDescription.setText(mobel.getDescription());
		tvGeoLocation.setText(mobel.getGeoLocation());
		
		
		
		 list = Inventory.inventorySiteList.get(mobel.getId());
		
		Log.i("NCS","Size is  :"+list.size());
		
		SiteAdapter adapter = new SiteAdapter(getActivity(),list);
		lv.setAdapter(adapter);
		
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
		
		
		displaySiteDetails(position);
		
	}
	
	
	private void displaySiteDetails(int position){
		
		warehouseDetails.setVisibility(View.GONE);
		siteDetails.setVisibility(View.VISIBLE);
		InventoryModel bean = list.get(position);
		
		
		tvSiteName.setText(bean.getSiteName());
		tvCity.setText(bean.getCity());         
		tvState.setText(bean.getState());
		tvStreet.setText(bean.getStreet());
		tvCountry.setText(bean.getCountry());
		tvZip.setText(bean.getZip());
		tvSitePhone.setText(bean.getPhone());
		tvSiteFax.setText(bean.getFax());
		tvEmail.setText(bean.getEmail());
		tvWebsite.setText(bean.getWebsite());
		
		
	}

}
